package com.example.cmpe277stockdata.dataaccess;

import java.net.MalformedURLException;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.example.cmpe277stockdata.InitialTabContent;
import com.example.cmpe277stockdata.beans.UserBean;
import com.example.cmpe277stockdata.constants.DataConstants;
import com.microsoft.windowsazure.mobileservices.*;

public class AzureLogin {

	private MobileServiceClient mClient;
	Context ctx;
	boolean matches = false;
	UserBean returnUser = new UserBean();


	public AzureLogin(Context ctx) throws MalformedURLException {
		this.ctx = ctx;
		mClient = new MobileServiceClient(
				"https://stockdata.azure-mobile.net/",
				"gbRGSggWlHFQwsQmeMgEULUirbGHgk27", ctx);

	}

	public UserBean insertLogin(String userName, String password) {
		UserBean bean = new UserBean();
		bean.setUserName(userName);
		bean.setPassword(password);

		mClient.getTable(UserBean.class).insert(bean,
				new TableOperationCallback<UserBean>() {
					public void onCompleted(UserBean entity,
							Exception exception, ServiceFilterResponse response) {
						if (exception == null) {
							Toast.makeText(ctx, "Insert Success",
									Toast.LENGTH_LONG).show();
							if(entity != null)
							{
								returnUser = entity;
								Intent intent = new Intent(ctx,InitialTabContent.class);
						 		intent.putExtra(DataConstants.USER_DATA, entity);
						 		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						 		ctx.startActivity(intent);
							}
							
						} else { // Insert failed
							Toast.makeText(ctx, "Insert Failure",
									Toast.LENGTH_LONG).show();
						}
					}
				});
		return returnUser;
	}
	
	public UserBean getUserDetails(String userName)
	{
		final UserBean returnBean = new UserBean();
        MobileServiceTable<UserBean> table = mClient.getTable("UserBean", UserBean.class);
        table.where().field("userName").eq(userName).execute(new TableQueryCallback<UserBean>() {

	            							              	@Override
															public void onCompleted(
																	List<UserBean> checkeduser,
																	int arg1,
																	Exception exception,
																	ServiceFilterResponse arg3) {
	            							              		if (exception == null) {
	            							              			if(checkeduser == null || checkeduser.size() < 1)
	            							              			{

	            							              			}
	            							              			else
	            							              			{
		            							              			returnBean.setUserName(checkeduser.get(0).getUserName());
		            							              			returnBean.setPassword(checkeduser.get(0).getPassword());
		            							              			returnBean.setId(checkeduser.get(0).getId());
	            							              			}
	            							              			
	            							                    } else {
	            							                    	
	            							                        }

																
															}
	            							            });
        
      
		return returnBean;
	}
	
	public boolean validateUser(String userName, String password)
	{
		final String passwd = password;
        MobileServiceTable<UserBean> table = mClient.getTable("UserBean", UserBean.class);
        table.where().field("userName").eq(userName).execute(new TableQueryCallback<UserBean>() {

             	@Override
				public void onCompleted(
						List<UserBean> checkeduser,
						int arg1,
						Exception exception,
						ServiceFilterResponse arg3) {
             		if (exception == null) {
             			if(checkeduser == null || checkeduser.size() < 1)
             			{
             				matches = false;
         					Toast.makeText(ctx, "Already registered? Not able to locate your login.",
									Toast.LENGTH_LONG).show();
             			}
             			else if(checkeduser != null && checkeduser.size() > 0)
             			{
             				if(checkeduser.get(0).getPassword().equals(passwd))
             				{
             					matches = true;
             					UserBean user = checkeduser.get(0);
           		 		 
             					Intent intent = new Intent(ctx,InitialTabContent.class);
             					intent.putExtra(DataConstants.USER_DATA, user);
             					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             					ctx.startActivity(intent);
             				}
             				else
             				{
             					Toast.makeText(ctx, "Username/Password dont match",
    									Toast.LENGTH_LONG).show();
             				}
             			}
                   } else {
                	  matches = false;
                   
                       }

					
				}
           });
		return matches;
	}

}
